layui.use(['table', 'form', 'jquery', 'common', 'laydate'], function() {
			let table = layui.table;
			let form = layui.form;
			let $ = layui.jquery;
			let common = layui.common;
			let laydate = layui.laydate;

			let cols = [
				[{
					field: "id",
					title: "ID",
					unresize: "true",
					align: "center"
				}, {
					field: 'name',
					title: '套餐名称',
					width: 200,
					align: 'center',
				}, {
					field: "days",
					title: "套餐天数",
					unresize: "true",
					align: "center"
				}, {
					field: "quota",
					title: "套餐额度",
					unresize: "true",
					align: "center",
				}, {
					field: "price",
					title: "套餐价格",
					unresize: "true",
					align: "center"
				}, {
					field: "status",
					title: "状态",
					unresize: "true",
					align: "center",
					templet: "#status"
				},{
					title: '操作',
					toolbar: '#options',
					unresize: true,
					align: 'center',
					width: 180,
				}]
			]

			table.render({
				elem: '#news',
				id: 'news',
				url: '/h/ajax/PackagegetDataList',
				page: true,
				cols: cols,
				cellMinWidth: 100,
				skin: 'line',
				toolbar: '#toolbar',
				defaultToolbar: [{
					title: '刷新',
					layEvent: 'refresh',
					icon: 'layui-icon-refresh',
				}, 'filter', 'print', 'exports']
			});

			table.on('tool(news)', function(obj) {
				if (obj.event === 'remove') {
					window.remove(obj);
				} else if (obj.event === 'edit') {
					window.edit(obj);
				}
			});

			table.on('toolbar(news)', function(obj) {
				if (obj.event === 'add') {
					window.add();
				} else if (obj.event === 'refresh') {
					window.refresh();
				}
			});


			// 修改状态
			form.on("switch(status)", function(data) {
				var id = this.value;
				var load = layer.load();
				$.ajax({
					data:{
					    id:id
					},
					url: "/h/ajax/Packagestatus",
					dataType: 'json',
					
					type: 'post',
					success: function(res) {
					    layer.close(load);
						if (res.code == 200) {
							layer.msg(res.msg, {
								icon: 1,
								time: 1000
							}, function() {
								layui.table.reload("news");
							});
						} else {
							layer.msg(res.msg, {
								icon: 2,
								time: 1000
							},function(){
							    layui.table.reload("news");
							});
						}
					},
					error:function(){
					    layer.close(load);
					    layer.msg("服务器错误！", {
							icon: 2,
							time: 1000
						},function(){
						    layui.table.reload("news");
						});
					}
				})
				return false;
				
			});
			//弹出窗设置 自己设置弹出百分比
			function screen() {
				if (typeof width !== 'number' || width === 0) {
					width = $(window).width() * 0.8;
				}
				if (typeof height !== 'number' || height === 0) {
					height = $(window).height() * 0.5;
				}
				return [width + 'px', height + 'px'];
			}

			// 添加数据
			window.add = function() {
				layer.open({
					type: 2,
					maxmin: true,
					title: '新增套餐管理',
					shade: 0.1,
					area: screen(),
					content: "Package/add"
				});
			}

			// 发送添加数据内容
			form.on('submit(addSave)', function(data) {
				$.ajax({
					data: JSON.stringify(data.field),
					url: "/h/ajax/add_Package",
					contentType: 'application/json',
					dataType: 'json',
					type: 'post',
					success: function(res) {
						if (res.code == 200) {
							layer.msg(res.msg, {
								icon: 1,
								time: 1000
							}, function() {
								parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
								parent.layui.table.reload("news");
							});
						} else {
							layer.msg(res.msg, {
								icon: 2,
								time: 1000
							});
						}
					},
					error:function(){
					    layer.msg("服务器错误！", {
							icon: 2,
							time: 1000
						});
					}
				})
				return false;
			});

			// 修改数据
			window.edit = function(obj) {
				layer.open({
					type: 2,
					maxmin: true,
					title: '修改套餐管理',
					shade: 0.1,
					area: screen(),
					content: 'Package/edit?id=' + obj.data['id']
				});
			}

			// 发送修改数据内容
			form.on('submit(editSave)', function(data) {
				$.ajax({
					data: JSON.stringify(data.field),
					url: "/h/ajax/edit_Package",
					contentType: 'application/json',
					dataType: 'json',
					type: 'post',
					success: function(res) {
						if (res.code == 200) {
							layer.msg(res.msg, {
								icon: 1,
								time: 1000
							}, function() {
								parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
								parent.layui.table.reload("news");
							});
						} else {
							layer.msg(res.msg, {
								icon: 2,
								time: 1000
							});
						}
					},
					error:function(){
					    layer.msg("服务器错误！", {
							icon: 2,
							time: 1000
						});
					}
				})
				return false;
			});

			// 数据删除
			window.remove = function(obj) {
				layer.confirm('确定要删除该套餐', {
					icon: 3,
					title: '提示'
				}, function(index) {
					layer.close(index);
					let loading = layer.load();
					$.ajax({
						url: '/h/ajax/Packageremove',
						data: {
							id: obj.data['id']
						},
						dataType: 'json',
						type: 'POST',
						success: function(res) {
							layer.close(loading);
							//判断有没有权限
							if (res.code == 200) {
								layer.msg(res.msg, {
									icon: 1,
									time: 1000
								}, function() {
									obj.del();
									layui.table.reload("news");
								});
							} else {
								layer.msg(res.msg, {
									icon: 2,
									time: 1000
								});
							}
						},
    					error:function(){
    					    layer.close(loading);
    					    layer.msg("服务器错误！", {
    							icon: 2,
    							time: 1000
    						});
    					}
					})
				});
			}

			window.refresh = function(param) {
				table.reload('news');
			}
		})